package cn.liujinnan.tetris.service;/*
 *
 * Copyright(c) 2021 by liujinnan.cn All rights Reserved.
 */

import cn.liujinnan.tetris.cache.TetrisPropertiesCache;
import cn.liujinnan.tetris.constant.TetrisPropertiesKeyEnum;

import javax.swing.*;
import java.awt.*;

/**
 * 修改设置后统一重画窗口
 *
 * @author LiuJinan
 * @date 2021-08-29 17:47
 */
public class ReloadService {

    /**
     * 设置主窗口大小
     */
    public void reloadWindowSize(JFrame jFrame) {
        TetrisPropertiesCache propertiesCache = TetrisPropertiesCache.getInstance();
        String windowFactor = propertiesCache.getValue(TetrisPropertiesKeyEnum.TETRIS_WINDOW_DEFAULT_FACTOR.getKey());
        Double aDouble = Double.valueOf(windowFactor);
        Integer width = propertiesCache.getIntValue(TetrisPropertiesKeyEnum.TETRIS_WINDOW_WIDTH.getKey());
        Integer height = propertiesCache.getIntValue(TetrisPropertiesKeyEnum.TETRIS_WINDOW_HEIGHT.getKey());
        jFrame.setSize((int)(width * aDouble), (int)(height * aDouble));

        // 窗口居中显示
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        //获取屏幕大小
        Dimension dim = toolkit.getScreenSize();
        jFrame.setLocation((dim.width-width) / 2, (dim.height-height) / 2);
    }
}
